//
//  MineDetailViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "MineDetailViewController.h"
#import "FLSREcommendHeadView.h"

#import "ScenicCollectViewController.h"
#import "DestinationCollectViewController.h"

@interface MineDetailViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) FLSREcommendHeadView *naviView;
@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, assign) CGPoint center;
@property (nonatomic, strong) ScenicCollectViewController *scVC;
@property (nonatomic, strong) DestinationCollectViewController *dcVC;

@end

@implementation MineDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"mine_navbar.jpg"] forBarMetrics:(UIBarMetricsDefault)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController.navigationBar setTranslucent:NO];
    self.view.backgroundColor = [UIColor whiteColor];
   
    [self addSubViews];
     [self setNavigationBar];
}

- (void)setNavigationBar
{
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionRightButton:)];
   
    self.navigationItem.rightBarButtonItem =rightButton;
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backNew.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeft:)];
  
    self.navigationItem.leftBarButtonItem = leftButton;
}

// 添加视图
- (void)addSubViews
{
    // 导航栏设置
    self.naviView = [[FLSREcommendHeadView alloc] initWithFrame:CGRectMake((self.view.width - 240) / 2, 20, 240, 28)];
    self.naviView.layer.cornerRadius = 14;
    self.naviView.layer.masksToBounds = YES;
    self.naviView.alpha = 0.5;
    self.naviView.layer.borderWidth = 0.5;
    self.naviView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.center = self.naviView.motionView.center;
    [self.naviView.hotCity addTarget:self action:@selector(actionHotC:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.naviView.hotCity setTitle:@"景点" forState:normal];
    [self.naviView.funnySeason addTarget:self action:@selector(actionFunnyS:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.titleView = self.naviView;
    [self.naviView.funnySeason setTitle:@"目的地" forState:normal];
    
    // 添加大背景scrollView
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , kScreenHeight - 49)];
    _bgScrollView.contentSize = CGSizeMake(kScreenWidth * 2, kScreenWidth - 96); // 滚动范围
    _bgScrollView.bounces = NO; // 不回弹
    _bgScrollView.pagingEnabled = YES; // 整屏滚动
    _bgScrollView.delegate = self;
    _bgScrollView.showsHorizontalScrollIndicator = NO; // 不显示水平滚动条
    [self.view addSubview:_bgScrollView];
    
    // 添加子控制器,并显示视图
    // 精选
    self.scVC = [[ScenicCollectViewController alloc] init];
    [self addChildViewController:self.scVC];
    [self.bgScrollView addSubview:self.scVC.view];
    self.dcVC = [[DestinationCollectViewController alloc] init];
    [self addChildViewController:self.dcVC];
    self.dcVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
    [self.bgScrollView addSubview:self.dcVC.view];
}

#pragma mark --navigationBar的点击方法
- (void)actionRightButton:(UIBarButtonItem *)rightButton
{
    self.scVC.tableView.editing = !self.scVC.tableView.editing;
    self.dcVC.tableView.editing = !self.dcVC.tableView.editing;
    if (self.scVC.tableView.editing) {
        rightButton.title = @"完成";
    } else {
        rightButton.title = @"编辑";
    }
}


- (void)actionLeft:(UIBarButtonItem *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 顶部导航栏的button实现
- (void)actionHotC:(UIButton *)button
{
    self.naviView.motionView.center = self.center;
    [self.naviView.funnySeason setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.naviView.hotCity setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    _bgScrollView.contentOffset = CGPointMake(0, 0) ;
}

- (void)actionFunnyS:(UIButton *)button
{
    self.naviView.motionView.center = CGPointMake(self.center.x + 120, self.center.y);
    [self.naviView.hotCity setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.naviView.funnySeason setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    _bgScrollView.contentOffset = CGPointMake(_bgScrollView.width * 1, 0) ;
}



#pragma mark -- scrollView协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat dx = scrollView.contentOffset.x * 120 / kScreenWidth ;
    CGPoint tempCenter = self.center;;
    tempCenter.x = self.center.x + dx;
    self.naviView.motionView.center = tempCenter;
    if (scrollView.contentOffset.x > 0) {
        [self.naviView.funnySeason setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [self.naviView.hotCity setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    } else {
        [self.naviView.hotCity setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [self.naviView.funnySeason setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
